// scan.js
// 移动动画
var util = require('../../utils/util.js')
var app = getApp();
var md5 = require('../md5.js');
let animation = wx.createAnimation({});
// 提示音
let innerAudioContext = wx.createInnerAudioContext()
//innerAudioContext.src = '/images/beep.mp3'

Page({
  data: {
  },
  onLoad: function (options) {
    var viewStyle = options.viewStyle
    if (viewStyle==0){
      this.setData({
        showView0: true
      });
      setTimeout(() => {
        this.setData({
          showView0: false
        });
        wx.reLaunch({
          url: '../aiIndex/aiIndex'
        })
      }, 1500);
    }
    if (viewStyle == 1) {
      this.setData({
        showView1: true
      });
      setTimeout(() => {
        this.setData({
          showView1: false
        });
        wx.reLaunch({
          url: '../aiIndex/aiIndex'
        })
      }, 1500);
    }
    if (viewStyle == 2) {
      this.setData({
        showView2: true
      });
      setTimeout(() => {
        this.setData({
          showView2: false
        });
        wx.reLaunch({
          url: '../aiIndex/aiIndex'
        })
      }, 1500);
    }
  },
  onPullDownRefresh: function(){
    wx.stopPullDownRefresh({
      complete: (res) => {},
    });
  },
  onShow(){
    this.donghua()
  },
  donghua(){
    var that = this;
	// 控制向上还是向下移动
    let m = true
	
    setInterval(function () {
      if (m) {
        animation.translateY(200).step({ duration: 3000 })
        m = !m;
      } else {
        animation.translateY(10).step({ duration: 3000 })
        m = !m;
      }

      that.setData({
        animation: animation.export()
      })
    }.bind(this), 3000)
  },
  toAindex: function () {
    wx.reLaunch({
      url: '../aiIndex/aiIndex'
    })
  },
  scancode(e){
    var that = this
    // 提示音
    //innerAudioContext.play()
    // 校验扫描结果，并处理
    let res = e.detail.result+''
    console.log('rescode='+res)
    //判断扫描结果
    var token=''
    if (res.indexOf('xtra_token') >= 0){
      console.log('aaaaaaaa')
      token = (res.split(':'))[1]
      token = token.substring(0, token.length - 1).replace(/\"/g, "")
      //获取缓存展会id和用户id存在则获取最新联系人列表
      var exhibitionid = wx.getStorageSync('exhibitionid')
      var u = wx.getStorageSync('u')
      console.log('exhibitionid=', exhibitionid)
      console.log('u=', u)
      console.log('token=', token)
      if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
        && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
        //默认最新（调用pico接口）
        var timestamp = Date.parse(new Date());
        timestamp = timestamp / 1000;
        var url = app.globalData.picoaddress + '/qrcodecheck/scanlogin/scanned'
        var k = {}
        k["v"] = '1.0.0'
        k["client"] = 'ios'
        k["timestamp"] = timestamp
        k["lan"] = 'cn'
        k["u"] = u
        k["token"] = token
        var sign = app.globalData.picotoken + 'clientios'+ 'timestamp' + timestamp +'token' + token + app.globalData.picotoken
        console.log(sign)
        var curb64 = md5.hexMD5(sign)
        console.log(curb64)
        k["sign"] = curb64
        wx.request({
          header: {
            'content-type': 'application/x-www-form-urlencoded',
          },
          url: url,
          method: 'post',
          data: k,
          success(res) {
        //    console.log(res)    
            wx.navigateTo({
              url: '../confrimLogin/website?token=' + token,
            })        
                      
          }, fail: res => {
            console.log(res, "error")
          }

        })
      }

    }else{
      //获取缓存展会id和用户id存在则获取最新联系人列表
      console.log('bbbbb')
      token = res
      var exhibitionid = wx.getStorageSync('exhibitionid')
      var u = wx.getStorageSync('u')
      console.log('exhibitionid=', exhibitionid)
      console.log('u=', u)
      if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
        && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
        //默认最新（调用pico接口）
        var timestamp = Date.parse(new Date());
        timestamp = timestamp / 1000;
        //添加联系人
        var url = app.globalData.picoaddress + '/attendee/contacts/scanadd'
        var k = {}
        k["v"] = '1.0.0'
        k["client"] = 'ios'
        k["timestamp"] = timestamp
        k["lan"] = 'cn'
        k["exhibitionid"] = exhibitionid
        k["u"] = u
        k["scancode"] = token
        var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'scancode' + token + 'timestamp' + timestamp + app.globalData.picotoken
        console.log(sign)
        var curb64 = md5.hexMD5(sign)
        console.log(curb64)
        k["sign"] = curb64
        wx.request({
          header: {
            'content-type': 'application/x-www-form-urlencoded',
          },
          url: url,
          method: 'post',
          data: k,
          success(res) {
            if(res.code==200){
              that.setData({
                showView0: true
              });
              setTimeout(() => {
                that.setData({
                  showView0: false
                });
              }, 2000);  
            }else{
              that.setData({
                showView2: true
              });
              setTimeout(() => {
                that.setData({
                  showView2: false
                });
              }, 2000);  
            }

          }, fail: res => {
            console.log(res, "error")
          }

        })
      }

    }
  }
})
